<template>
  <el-row type="flex" justify="space-between" align="middle" style="height:40px;width:100%">
    <el-col>
      <!-- 名称应该变成 对应的节点中的name -->
      <span>{{ data.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ data.manager }}</el-col>
        <el-col>
          <el-dropdown @command="handleCommand">
            <span>操作<i class="el-icon-arrow-down" /></span>
            <el-dropdown-menu>
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="view">查看部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown></el-col>
      </el-row></el-col>
  </el-row>
</template>

<script>
import { delDeapartApi } from '@/api/departments'
export default {
  name: 'Treetools',
  // props可以用数组来接受数据，也可以用对象来接受
  // props:{props属性：{配置选项}}
  props: ['data', 'isRoot'],
  methods: {
    async  handleCommand(command) {
      // 判断
      if (command === 'add') {
        // 添加子部门的操作
        // 告诉父组件显示弹层
        this.$emit('opendialog', { data: this.data, process: command })
      } else if (command === 'del') {
        // 删除操作
        try {
          // 询问是否删除
          await this.$confirm('是否删除当前部门')
          // 提交数据到服务器
          await delDeapartApi(this.data.id)
          // 添加提示信息
          this.$message.success('删除部门成功')
          // 更新数据:调用父组件中的方法
          this.$emit('updatedata')
        } catch {
          console.log('取消删除&删除出错')
        }
      } else {
        this.$emit('opendialog', { data: this.data, process: command })
      }
    }
  }
}
</script>

<style less="scss">

</style>
